﻿using MenuService.Entities;
using MenuService.Enums;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace MenuService.Manager
{
    public class MenuManager : DomainService
    {
        private readonly IRepository<AbpMenu, Guid> _menuRepository;

        public MenuManager(IRepository<AbpMenu, Guid> menuRepository)
        {
            this._menuRepository = menuRepository;
        }

        public async Task<AbpMenu> CreateAsync(Guid? parentId, string? parentIds, [NotNull] string name, [NotNull] string code, MenuType type, string icon, string router, string componet, [NotNull] string permission, [NotNull] string application, MenuOpenType openType, bool isVisible, string link, string redirect, MenuWeight menuWeight, int sort, string remark, CommonStatus commonStatus)
        {
            var isExists = await _menuRepository.AnyAsync(x => x.Name == name || x.Code == code || x.Permission == permission);
            if (!isExists) throw new UserFriendlyException("invalid name or code or permission");

            return await _menuRepository.InsertAsync(new AbpMenu(GuidGenerator.Create(), parentId, parentIds, name, code, type, icon, router, componet, permission, application, openType, isVisible, link, redirect, menuWeight, sort, remark, commonStatus));
        }

        //public async Task<AbpMenu> EditAsync(Guid id, Guid? parentId, string? parentIds, [NotNull] string name, [NotNull] string code, MenuType type, string icon, string router, string componet, [NotNull] string permission, [NotNull] string application, MenuOpenType openType, bool isVisible, string link, string redirect, MenuWeight menuWeight, int sort, string remark, CommonStatus commonStatus)
        //{
        //    var entity = await _menuRepository.FirstOrDefaultAsync(x => x.Id == id);
        //    if (entity == null) throw new UserFriendlyException("menu does not exists");
        //    if (entity.Name ==name&&entity.Code==code&&entity.Permission==permission)
        //    {
        //        //
        //    }
        //    var isExistEntity = await _menuRepository.AnyAsync(x => x.)
        //}
    }
}
